
import { createApp } from 'vue'
import App from './App.vue'

import router from './js/router'

import { createPinia } from 'pinia'
import piniaPluginPersist from 'pinia-plugin-persist'

import ElementPlus from 'element-plus'
import { ElMessage } from 'element-plus' 
import 'element-plus/dist/index.css'
// 配置中文语言包
import zhCn from 'element-plus/es/locale/lang/zh-cn.mjs'
// 导入Day.js中文语言包
import 'dayjs/locale/zh-cn'

import axios from '@/js/axios'
import VueAxios from 'vue-axios'

// 导入错误修复CSS
import './css/errorFix.css'

// 添加ResizeObserver错误捕获
const originalError = window.console.error;
window.console.error = function(msg) {
  if (msg.toString().includes('ResizeObserver') || 
      msg.toString().includes('loop completed with undelivered notifications')) {
    // 忽略ResizeObserver错误
    return;
  }
  originalError.apply(this, arguments);
};

// 添加全局错误事件监听
window.addEventListener('error', (event) => {
  // 处理ResizeObserver相关错误
  if (event && event.message && 
      (event.message.includes('ResizeObserver') || 
       event.message.includes('undelivered notifications'))) {
    // 阻止错误冒泡
    event.stopPropagation();
    event.preventDefault();
    return false;
  }
}, true);

// 将ElMessage添加到全局，方便在模板中直接使用
window.ElMessage = ElMessage;

const app = createApp(App);

// 添加全局错误处理
app.config.errorHandler = (err, vm, info) => {
  // 忽略ResizeObserver错误
  if (err.message && (
      err.message.includes('ResizeObserver') || 
      err.message.includes('loop completed with undelivered notifications')
    )) {
    return;
  }
  console.error('Vue错误:', err, info);
};

// 阻止未处理的Promise错误显示
window.addEventListener('unhandledrejection', event => {
  // 处理Chunk加载错误
  if (event.reason && event.reason instanceof Error && 
      event.reason.message && event.reason.message.includes('ChunkLoadError')) {
    console.log('检测到资源加载错误，尝试刷新页面...');
    ElMessage.error('资源加载失败，正在尝试刷新页面');
    
    // 清除资源缓存
    if (window.caches) {
      caches.keys().then(keys => {
        keys.forEach(key => {
          caches.delete(key);
        });
      });
    }
    
    // 延迟300ms后刷新页面
    setTimeout(() => {
      window.location.reload();
    }, 300);
    
    event.preventDefault();
    event.stopPropagation();
    return;
  }
  
  if (event.reason && 
      (String(event.reason).includes('ResizeObserver') || 
       String(event.reason).includes('loop completed with undelivered notifications'))) {
    event.preventDefault();
    event.stopPropagation();
  }
});

app.use(router)
  .use(ElementPlus, { locale: zhCn })
  .use(VueAxios, axios)
  .use(createPinia().use(piniaPluginPersist))
  .mount('#app')

